﻿using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using System.IO.Ports;
using System.Diagnostics;
using Newtonsoft.Json;
using System.IO;
using Newtonsoft.Json.Linq;
using System.Reflection;
using System.Net.Sockets;

namespace oled
{
    public partial class Simulator : Form
    {
        Bitmap oled_bitmap;
        
        bool oled_Refresh;
        Image oled_image;

        public Simulator()
        {
            InitializeComponent();
        }

        private void Control_visible(bool visiable)
        {
            this.oled_panel.Visible = !visiable;
            this.Height_Text.Visible = visiable;
            this.Width_Text.Visible = visiable;
            this.FPS_Text.Visible = visiable;
            this.label_Use_Serial.Visible = visiable;
            this.label_Height.Visible = visiable;
            this.label_Width.Visible = visiable;
            this.label_FPS.Visible = visiable;
            this.PortList.Visible = visiable;
            this.Enter.Visible = visiable;
            this.label_Prot.Visible = visiable;
            this.Port.Visible = visiable;
        }

        // 获取串口号
        private void SerialPort_Get()
        {
            PortList.Items.Clear();
            string[] PortGet = SerialPort.GetPortNames();                  //port list add
            PortList.Items.Add("None");
            if (PortGet.Length != 0 && PortGet != null)                    //数组为空，且长度为0  (判断数组为空的方法)
            {
                for (Byte i = 0; i < PortGet.Length; i++)
                {
                    PortList.Items.Add(PortGet[i]);
                }
                PortList.SelectedIndex = 0;
            }
        }

        // 键号 ESC
        private void Esc_down()
        {
            this.timer1.Enabled = !Enabled;
            this.BackColor = SystemColors.GradientInactiveCaption;
            WinForm_Contrl.Windows_Set_Center(
                this, 200, 170,
                this.oled_panel, 160, 160
                );
            Control_visible(true);
        }
        // 键号 事件处理
        private void KeyBoard_Event(int keyvalue)
        {
            switch (keyvalue)
            {
                case 8:
                    break;
                case 65:
                    break;
                case 66:
                    break;
                case 27:
                    Esc_down();
                    break;
                case 37: //Left
                    break;
                case 38: //UP
                    break;
                case 39: //Right
                    break;
                case 40: //Down
                    break;
                case 84: // T
                    this.timer1.Enabled = Enabled;// !this.timer1.Enabled;
                    break;
                default:
                    break;
            }
        }

        // 确认按钮事件
        private void Enter_Button_Click(object sender, EventArgs e)
        {
            bool height = int.TryParse(Height_Text.Text, out int OLED_Height);
            bool width = int.TryParse(Width_Text.Text, out int OLED_Width);
            //bool visiable = this.Enter.Visible;

            if (height && width && OLED_Height >= 32 && OLED_Width >= 32)
            {
                OLED_Drive oled = new OLED_Drive();
                oled.Set_PaintPanel(ref OLED_Height, ref OLED_Width);
                
                oled_bitmap = new Bitmap(OLED_Width, OLED_Height);//像素格式为每像素 1 位
                BackColor = SystemColors.HotTrack;
                WinForm_Contrl.Windows_Set_Center(
                    this, OLED_Height + 52, OLED_Width + 30,
                    oled_panel, OLED_Height, OLED_Width
                    );
                Control_visible(false);
                TCP_Server_init();
            }
        }

        // 键盘按键事件
        private void oled_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.oled_panel.Visible == true)
            {
                KeyBoard_Event(e.KeyValue);
            }
        }

        // 点击屏幕事件
        private void oled_Click(object sender, EventArgs e)
        {
            //Oled_Drive.Clear(this.oled_panel);
        }

        private void Simulator_Load(object sender, EventArgs e)
        {
            SerialPort_Get(); 
            this.KeyPreview = true;
            this.KeyDown += new KeyEventHandler(oled_KeyDown);
        }

        private void SerialPort_Click(object sender, EventArgs e)
        {
            SerialPort_Get();
        }

        private void Timer1_Tick(object sender, EventArgs e)
        {

            if (oled_Refresh)
            {
                OLED_Drive oled = new OLED_Drive();
                oled.Refresh(oled_bitmap);
                oled_panel.Image = oled_bitmap;
                oled_panel.Refresh();
                oled_Refresh = false;
            }
            GC.Collect();  
        }
    }
}
